package com.xing.binbackend.pattern.factory12.ecommerce;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

// 自动注册工厂（Spring风格）
@Component
public class PaymentRegistry {
    private Map<String, Payment> payments = new ConcurrentHashMap<>();

    @Autowired
    public PaymentRegistry(List<Payment> paymentList) {
        paymentList.forEach(payment -> {
            payments.put(payment.getClass().getSimpleName().toLowerCase(), payment);
        });
    }

    public Payment getPayment(String type) {
        return payments.get(type.toLowerCase());
    }
}
